package org.jeecg.modules.nurse_user_orders.common;


import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.boot.starter.rabbitmq.core.BaseRabbiMqHandler;
import org.jeecg.boot.starter.rabbitmq.listenter.MqListener;
import org.jeecg.common.annotation.RabbitComponent;
import org.jeecg.common.base.BaseMap;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.constant.rabbitConstant;
import org.jeecg.modules.nurse_user_orders.service.INurseUserOrdersService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;

@Slf4j
@RabbitComponent(value = "ordersReceiver")
public class orderReceiver extends BaseRabbiMqHandler<BaseMap> {

    @Autowired
    private INurseUserOrdersService nurseUserOrdersService;

    @RabbitListener(queues = rabbitConstant.DELAY_QUEUE_ORDER_NAME, ackMode = "MANUAL")
    public void onMessageOrder(BaseMap baseMap, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
        super.onMessage(baseMap, deliveryTag, channel, new MqListener<BaseMap>() {

            @Override
            public void handler(BaseMap map, Channel channel) {

                try {
                    if (oConvertUtils.isEmpty(map)) {
                        return;
                    }
                    if (oConvertUtils.isEmpty(map.get("out_trade_no"))) {
                        return;
                    }
                    log.info("【支付延迟事务】MQ out_trade_no:{}", map.get("out_trade_no").toString());
                    nurseUserOrdersService.checkOrderStatus(map.get("out_trade_no").toString());
                } catch (Exception e) {
                    log.error("【支付延迟事务】错误:{}", e);
                }

            }
        });
    }
}
